package com.yunkaweilai.android.view.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.classic.common.MultipleStatusView;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.interfaces.MyDialogInterfaceForCoupon;
import com.yunkaweilai.android.model.CodeMsgBean;
import com.yunkaweilai.android.model.CouponSearchListBean;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.MyToast;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;

/**
 * 优惠券列表弹窗
 */

public class CouponListDialog extends Dialog implements RadioGroup.OnCheckedChangeListener, MyDialogInterfaceForCoupon {

    private RadioGroup RGB;
    private RadioButton RbM;
    private RadioButton RbP;
    private ListView listView;
    private ListView listView2;
    private LinearLayout lineEmpty;
    private LinearLayout imageViewClose;
    private TextView mTextEmptyText;
    private MultipleStatusView id_multipleStatusView;
    private CommonAdapter<CouponSearchListBean.DataBean.ListBean.CashCouponBean> adapter;//优惠券的两个
    private ArrayList<CouponSearchListBean.DataBean.ListBean.CashCouponBean> listBeans = new ArrayList<>();
    private CommonAdapter<CouponSearchListBean.DataBean.ListBean.MemberCouponListBean> adapter2;//查看个人拥有优惠券的两个
    private ArrayList<CouponSearchListBean.DataBean.ListBean.MemberCouponListBean> list = new ArrayList<>();
    private Context context;
    private String member_id;
    private String yhq_id;
    private String Type = "CashCoupon";
    private boolean sms = false;
    private boolean wechat = false;

    public CouponListDialog(Context context, String member_id, String name) {
        super(context);
        this.context = context;
        this.member_id = member_id;
    }

    public CouponListDialog(Context context, String member_id, String name, boolean sms, boolean wechat) {
        super(context);
        this.context = context;
        this.member_id = member_id;
        this.sms = sms;
        this.wechat = wechat;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_coupon_list);
        RGB = findViewById(R.id.id_rgp_selecotr);
        RbM = findViewById(R.id.id_rb_yhq);
        RbP = findViewById(R.id.id_rb_see);
        listView = findViewById(R.id.id_listView);
        listView2 = findViewById(R.id.id_listView2);
        lineEmpty = findViewById(R.id.line_empty);
        mTextEmptyText = findViewById(R.id.id_coupon_empty_text);
        imageViewClose = findViewById(R.id.id_img_close);
        id_multipleStatusView = findViewById(R.id.id_multipleStatusView);
        id_multipleStatusView.showLoading();
        id_multipleStatusView.setOnRetryClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                id_multipleStatusView.showLoading();
                inithttp();
            }
        });
        initclick();
        initAdapter();
        inithttp();
        RbM.setClickable(false);
        RbP.setClickable(false);
        RbM.setChecked(true);
    }

    private void initclick() {
        //设置radiogroup切换事件
        RGB.setOnCheckedChangeListener(this);
        //关闭弹窗
        imageViewClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        //
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                yhq_id = listBeans.get(i).getId();
                MyDialogForCoupon myDialog = new MyDialogForCoupon(context, CouponListDialog.this, "提示", listBeans.get(i).getTitle() + "<Br/>" + "确定要发送吗？", sms, wechat);
                myDialog.show();
                hide();
            }
        });
    }


    private void initAdapter() {
        //发送优惠券和折扣券的adapter
        adapter = new CommonAdapter<CouponSearchListBean.DataBean.ListBean.CashCouponBean>(context, R.layout.item_list_popup_coupon_dialog, listBeans) {
            @Override
            protected void convert(ViewHolder viewHolder, CouponSearchListBean.DataBean.ListBean.CashCouponBean item, int position) {

                viewHolder.setText(R.id.id_tv_type_full_price, "满" + item.getFull_price() + "元即可使用");
                if (item.getCoupon_type().equals("CashCoupon")) {
                    viewHolder.setText(R.id.id_tv_type_type, "代金券");
                    viewHolder.setText(R.id.id_tv_type_dai_price, "¥" + Utils.ManageMoney(item.getDai_price()));
                } else if (item.getCoupon_type().equals("DiscountCoupon")) {
                    viewHolder.setText(R.id.id_tv_type_type, "折扣券");
                    viewHolder.setText(R.id.id_tv_type_dai_price, item.getDai_price() + "折");
                }
                viewHolder.setText(R.id.id_tv_type_name, item.getTitle());
                viewHolder.setText(R.id.id_tv_type_content, String.valueOf(Html.fromHtml("可发放：" + item.getCoupon_num() + "张<br>" + "有效期：" + item.getValidity_start() + "-" + item.getValidity_end())));
            }
        };
        //查看会员详情的adapter
        adapter2 = new CommonAdapter<CouponSearchListBean.DataBean.ListBean.MemberCouponListBean>(context, R.layout.item_list_popup_coupon_dialog, list) {
            @Override
            protected void convert(ViewHolder viewHolder, CouponSearchListBean.DataBean.ListBean.MemberCouponListBean item, int position) {
                viewHolder.setText(R.id.id_tv_type_full_price, "满" + item.getCoupon_full_price() + "元即可使用");
                if (item.getCoupon_type().equals("CashCoupon")) {
                    viewHolder.setText(R.id.id_tv_type_type, "代金券");
                    viewHolder.setText(R.id.id_tv_type_dai_price, "¥" + Utils.ManageMoney(item.getCoupon_dai_price()));
                } else if (item.getCoupon_type().equals("DiscountCoupon")) {
                    viewHolder.setText(R.id.id_tv_type_type, "折扣券");
                    viewHolder.setText(R.id.id_tv_type_dai_price, item.getCoupon_dai_price() + "折");
                }
                viewHolder.setText(R.id.id_tv_type_name, item.getCoupon_title());
                viewHolder.setText(R.id.id_tv_type_content, "有效期：" + item.getValidity_start() + "-" + item.getValidity_end());
            }
        };

        listView.setAdapter(adapter);
        listView2.setAdapter(adapter2);
    }

    private void inithttp() {
        HttpRequest.postUrl(Api.COUPON_SEARCH_LIST)
                .addParams("member_id", member_id)
                .execute(new PostProcess.StringCallBack() {
                    @SuppressLint("SetTextI18n")
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(context,json);
                        if (checkCode) {
                            RbM.setClickable(true);
                            RbP.setClickable(true);
                            listBeans.clear();
                            Gson gson = new Gson();
                            CouponSearchListBean couponSearchListBean = gson.fromJson(json, CouponSearchListBean.class);
                            listBeans.addAll(couponSearchListBean.getData().getList().getCashCoupon());
                            listBeans.addAll(couponSearchListBean.getData().getList().getDiscountCoupon());
                            list.addAll(couponSearchListBean.getData().getList().getMemberCouponList());
                            RbM.setText("发送优惠券(" + listBeans.size() + ")");
                            RbP.setText("会员已有(" + list.size() + ")");
                            adapter.notifyDataSetChanged();
                            adapter2.notifyDataSetChanged();
                            if (listBeans.size() > 0) {
                                if (id_multipleStatusView != null) {
                                    id_multipleStatusView.showContent();
                                }
                            } else {
                                if (id_multipleStatusView != null) {
                                    id_multipleStatusView.showContent();
                                }
                                lineEmpty.setVisibility(View.VISIBLE);
                            }
                        } else {
                            if (id_multipleStatusView != null) {
                                id_multipleStatusView.showError();
                            }
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        if (id_multipleStatusView != null) {
                            id_multipleStatusView.showError();
                        }
                        MyToast.showShortToast(context, e.getMessage());
                    }
                });
    }

    private void initSendYhq(String type) {
        HttpRequest.postUrl(Api.APP_PUSH_COUPON)
                .addParams("member_id", member_id)
                .addParams("coupon_id", yhq_id)
                .addParams("push_type", type)
                .executeToken(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkcode = Utils.checkCode(context,json);
                        Gson gson = new Gson();
                        if (checkcode) {
                            CodeMsgBean codeMsgBean = gson.fromJson(json, CodeMsgBean.class);
                            MyToast.showShortToast(context, codeMsgBean.getMessage());
                        } else {
                            CodeMsgBean codeMsgBean = gson.fromJson(json, CodeMsgBean.class);
                            MyToast.showShortToast(context, codeMsgBean.getMessage());
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        MyToast.showShortToast(context, e.getMessage());
                    }
                });
    }


    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.id_rb_yhq://优惠券
//                id_multipleStatusView.showLoading();
                if (listBeans.size() > 0) {
                    lineEmpty.setVisibility(View.GONE);
                    listView.setVisibility(View.VISIBLE);
                    listView2.setVisibility(View.GONE);
                } else {
                    lineEmpty.setVisibility(View.VISIBLE);
                    mTextEmptyText.setVisibility(View.VISIBLE);
                    listView.setVisibility(View.GONE);
                    listView2.setVisibility(View.GONE);
                }
                break;
            case R.id.id_rb_see://查看会员详情
                if (list.size() > 0) {
                    lineEmpty.setVisibility(View.GONE);
                    listView.setVisibility(View.GONE);
                    listView2.setVisibility(View.VISIBLE);
                } else {
                    mTextEmptyText.setVisibility(View.GONE);
                    lineEmpty.setVisibility(View.VISIBLE);
                    listView.setVisibility(View.GONE);
                    listView2.setVisibility(View.GONE);
                }
                break;
        }
    }

    @Override
    public void RightClick(String type) {
        initSendYhq(type);
        dismiss();
    }

    @Override
    public void CancelClick() {
        show();
    }


}
